package com.aqie.easy.bitOperation;

/**
 * 476. 数字的补数
 * 补数是对该数的二进制表示取反。
 * 1 & 0 = 0  0 & 1 = 0
 * 1 | 0 = 1  0 | 1 = 1
 * 1 ^ 0 = 1  1 ^ 1 = 0  0 ^ 1 = 1
 */
public class FindComplement {
    public static int findComplement(int num) {
        int tmp = num, c = 0;
        while(tmp > 0){
            // 右移相当于除2
            tmp >>= 1;
            System.out.println(tmp);
            c = (c << 1) + 1;
        }
        return num ^ c;
    }

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);                          // 2147483647
        System.out.println(Integer.toBinaryString(Integer.MAX_VALUE));  // 1111111111111111111111111111111
        // String 转int
        Integer.parseInt(Integer.toBinaryString(5));
        findComplement(5);
    }
}
